package com.lz.d;

import java.util.Scanner;

/**
 * @ClassName Demo36
 * @Description 斐波那契问题
 * @Author 2214361854
 * @Date 2022/5/27 16:27
 * @Version 1.0
 */
public class Demo36 {
    /**
     * 已知:斐波那契数列的前几个数分别为 0,1,1,2,3,5…
     * 从第三项开始,每一项都等于前两项的和
     * 请接收用户输入的整数n,求出此数列的前n项.
     */
    public static void main(String[] args) {
        System.out.println("输入整数n：");
        int n = new Scanner(System.in).nextInt();
        FibonacciFor(n);
    }

    private static void FibonacciFor(int n) {
        if (n < 1) {
            System.out.println("输入数据有误！！！");
            return;
        }
        //一位
        if (n == 1) {
            System.out.println(0);
            return;
        }
        int sum = 0;
        int a = 0;
        int b = 1;
        System.out.print("0 1 ");
        for (int i = 3; i <= n; i++) {
            sum = a + b;
            a = b;
            b = sum;
            System.out.print(sum + " ");
        }
    }
}
